package com.sky.demo.activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import com.sky.demo.R;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sky.demo.global.BaseActivity;
import com.sky.demo.utils.RegexUtils;
import com.sky.demo.utils.ToastUtils;
import com.sky.demo.views.RefreshListView;
/**
 * @author 彬 QQ 1136096189
 * @Description: listview重写，刷新与载入
 * @date 2015/8/17 15:30
 */
public class RefreshListActivity extends BaseActivity implements RefreshListView.OnRefreshListener {
	
	private RefreshListView mListView;
	private ArrayAdapter<String> mAdapter;
    private ArrayList<String> items = new ArrayList<String>();
    private Handler mHandler;
    private int mIndex = 0;
    private int mRefreshIndex = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_refresh);
		geneItems();
		initView();
		 
	}
	private void initView() {
        mHandler = new Handler();

        mListView = (RefreshListView) findViewById(R.id.list_view);
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
//        mListView.setAutoLoadEnable(true);
        mListView.setRefreshListener(this);
        mListView.setRefreshTime(getTime());

        mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
        mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				createDialog(new String[]{"18322301875","18322301596"});
				if (!pop.isShowing())
					pop.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
			}
		});
    }
	private void geneItems() {
		for (int i = 0; i != 6; ++i) {
			items.add("Test XListView item " + (++mIndex));
		}
	}
	
	private String getTime() {
        return new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(new Date());
    }
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);

//		if (hasFocus) {
//			mListView.autoRefresh();
//		}
	}

	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mIndex = ++mRefreshIndex;
				items.clear();
				geneItems();
				mAdapter = new ArrayAdapter<String>(RefreshListActivity.this,
						R.layout.list_item, items);
				mListView.setAdapter(mAdapter);
				onLoad();
			}
		}, 2500);
	}

	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				geneItems();
				mAdapter.notifyDataSetChanged();
				onLoad();
			}
		}, 2500);
	}


	private void onLoad() {
		mListView.stopRefresh();
		mListView.stopLoadMore();
		mListView.setRefreshTime(getTime());
	}
	private PopupWindow pop;

	private void createDialog(final String[] phone) {
		pop = new PopupWindow(this);
		View inflate = LayoutInflater.from(this).inflate(R.layout.pop_phone, null);
		pop.setAnimationStyle(R.style.AnimationFade);
		pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		pop.setBackgroundDrawable(new BitmapDrawable());
		pop.setFocusable(true);
		pop.setOutsideTouchable(true);
		pop.setContentView(inflate);

		RelativeLayout parent = (RelativeLayout) inflate.findViewById(R.id.parent);
		Button cancle = (Button) inflate
				.findViewById(R.id.item_popupwindows_cancel);
		TextView tv01 = (TextView) inflate.findViewById(R.id.phone01);
		TextView tv02 = (TextView) inflate.findViewById(R.id.phone02);
		tv01.setText(phone[0]);
		tv02.setText(phone[1]);
		tv01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (RegexUtils.isChinesePhoneNumber(phone[0]) || RegexUtils.isChineseTel(phone[0]))
					startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone[0])));
				else
					ToastUtils.showShort(RefreshListActivity.this, "电话号码错误");

				pop.dismiss();

			}
		});
		tv02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (RegexUtils.isChinesePhoneNumber(phone[1]) || RegexUtils.isChineseTel(phone[1]))
					startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone[1])));
				else
					ToastUtils.showShort(RefreshListActivity.this, "电话号码错误");
				pop.dismiss();

			}
		});
		inflate.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (pop != null && pop.isShowing()) {
					pop.dismiss();
					pop = null;
				}
				return true;
			}
		});
		parent.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				pop.dismiss();
			}
		});
		cancle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				pop.dismiss();
			}
		});
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();

	}
}
